package com.gof.example.prototype;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Created by zhiyuan on 2018/5/2 11:44.
 * haiming.fei@tongdun.cn
 */
public class Customer implements Cloneable, Serializable {

    private String fullName;
    private Address addr;

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Address getAddr() {
        return addr;
    }

    public void setAddr(Address addr) {
        this.addr = addr;
    }

    //浅拷贝
//    @Override
//    protected Customer clone() throws CloneNotSupportedException {
//        return (Customer)super.clone();
//    }


    //深拷贝
//    @Override
//    protected Customer clone() throws CloneNotSupportedException {
//        Customer c = (Customer) super.clone();
//        c.addr = (Address) addr.clone();
//        return c;
//    }


    //序列化拷贝
    @Override
    protected Customer clone() {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        try {
            ObjectOutputStream os = new ObjectOutputStream(bao);
            os.writeObject(this);

            ByteArrayInputStream bai = new ByteArrayInputStream(bao.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bai);
            Customer customer = (Customer) ois.readObject();
            return customer;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "fullName='" + fullName + '\'' +
                ", addr=" + addr +
                '}';
    }
}
